<?php
require_once 'Junction/Query.php';

class Junction_Query_Update implements Junction_Query {
	
	private $_tables;
	private $_columns;
	private $_params;
	/**
	 * @var Junction_Clause
	 */
	private $_clause;
	
	public function __construct() {
		$this->_tables = array();
		$this->_columns = array();
		$this->_params = array();
	}
	
	/**
	 * @param String $name
	 */
	public function bindTable($name) {
		$this->_tables[] = $name;
	}
	
	/**
	 * @param String $column
	 * @param unknown_type $value
	 */
	public function bindColumn($column, $value = null) {
		$this->_columns[] = $column;
		$this->_params[] = $value;
	}
	
	/**
	 * @param Junction_Clause $builder
	 */
	public function bindClause(Junction_Clause $builder) {
		$this->_clause = $builder;
	}
	
	/**
	 * @return String
	 */
	public function toSql() {
		$q = 'UPDATE ';
		$q .= implode($this->_tables, ', ');
		$q .= ' SET ';
		$q .= implode($this->_columns, ' = ?, ');
		$q .= ' = ?';
		if (isset($this->_clause)) {
			$q .= $this->_clause->toSql();
		}
		return $q;
	}
	
	/**
	 * @return array
	 */
	public function getParameters() {
		if (isset($this->_clause)) {
			return array_merge($this->_params, $this->_clause->getParameters());
		}
		return $this->_params;
	}
	
	/**
	 * @return String
	 */
	public function __toString() {
		return $this->toSql();
	}
}
?>